package liurong.driver.hashmap;

import java.util.HashMap;

import org.junit.jupiter.api.Test;

public class HashMapTest {

	static int MAXIMUM_CAPACITY = 1 << 30;

	@Test
	public void test1() {
		System.out.println(tableSizeFor(7));
		System.out.println(tableSizeFor(8));
		System.out.println(tableSizeFor(16));
		System.out.println(tableSizeFor(17));

		int a = 1 << 30;
		System.out.println(a);
		System.out.println(tableSizeFor(a));

	}

	static final int tableSizeFor(int cap) {
		int n = cap - 1;
		n |= n >>> 1;
		n |= n >>> 2;
		n |= n >>> 4;
		n |= n >>> 8;
		n |= n >>> 16;
		return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
	}

	@Test
	public void test2() {
		int[] arr = new int[10];

		int[] basic = arr;

		basic[0] = 1;

		System.out.println(arr[0]);
		System.out.println(arr[1]);
	}

	@Test
	public void test3() {
		HashMap<HashCodeTest, Integer> hashMap = new HashMap<HashCodeTest, Integer>(2);

		hashMap.put(null, 1);

		System.out.println(hashMap.get(null));

		for (int i = 0; i < 9; i++) {
			hashMap.put(new HashCodeTest(i), i);
		}
	}

	@Test
	public void test4() {
		HashMap<String, String> hashMap = new HashMap<>();
		hashMap.put("1", "1");
		hashMap.put("2", "2");
		hashMap.put("3", "3");
		hashMap.put("4", "4");

		Holder holder = new Holder();
		holder.setHashMap(hashMap);
		hashMap = holder.getHashMap();

		hashMap = new HashMap<>(hashMap);
		hashMap.remove("1");
		hashMap.remove("2");

		System.out.println(holder.getHashMap());
	}

	public class RpcHolder {
		private HashMap<String, String> hashMap;

		public HashMap<String, String> getHashMap() {
			return hashMap;
		}

		public void setHashMap(HashMap<String, String> hashMap) {
			this.hashMap = hashMap;
		}
	}

	public class Holder {
		private HashMap<String, String> hashMap;

		public HashMap<String, String> getHashMap() {
			return hashMap;
		}

		public void setHashMap(HashMap<String, String> hashMap) {
			this.hashMap = hashMap;
		}

	}
}
